<template>
  <div class="message-container">
    <div class="message-sidebar">
      <div class="sidebar-header">
        <h2>消息中心</h2>
      </div>
      <div class="sidebar-menu">
        <router-link 
          :to="`/home/message/${$route.params.id}/like`" 
          class="menu-item"
          active-class="active"
        >
          <div class="menu-item-content">
            <t-icon name="heart-filled" class="menu-icon like-icon" />
            <span>收到的赞</span>
            <div v-if="profile.unreadLikeCount > 0" class="unread-badge">{{ profile.unreadLikeCount }}</div>
          </div>
        </router-link>
        
        <router-link 
          :to="`/home/message/${$route.params.id}/reply`" 
          class="menu-item"
          active-class="active"
        >
          <div class="menu-item-content">
            <t-icon name="message-circle" class="menu-icon reply-icon" />
            <span>回复我的</span>
            <div v-if="profile.unreadCommentCount > 0" class="unread-badge">{{ profile.unreadCommentCount }}</div>
          </div>
        </router-link>
        
        <router-link 
          :to="`/home/message/${$route.params.id}/chat`" 
          class="menu-item"
          active-class="active"
        >
          <div class="menu-item-content">
            <t-icon name="mail" class="menu-icon chat-icon" />
            <span>私信我的</span>
            <div v-if="unreadCounts.chat > 0" class="unread-badge">{{ unreadCounts.chat }}</div>
          </div>
        </router-link>
        
        <router-link 
          :to="`/home/message/${$route.params.id}/system`" 
          class="menu-item"
          active-class="active"
        >
          <div class="menu-item-content">
            <t-icon name="bell" class="menu-icon system-icon" />
            <span>系统通知</span>
            <div v-if="unreadCounts.system > 0" class="unread-badge">{{ unreadCounts.system }}</div>
          </div>
        </router-link>
      </div>
    </div>
    
    <div class="message-content">
      <router-view />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useUserStore } from '@/stores/userStore';

const route = useRoute();
const router = useRouter();

// 获取 userStore 实例
const userStore = useUserStore();
const { profile } = storeToRefs(userStore);

// 未读消息计数
const unreadCounts = ref({
  like: 0,
  reply: 0,
  chat: 0,
  system: 0
});

// 模拟获取未读消息数量
const fetchUnreadCounts = () => {
  // 这里应该是从API获取未读消息数量
  // 暂时使用模拟数据
  unreadCounts.value = {
    like: Math.floor(Math.random() * 10),
    reply: Math.floor(Math.random() * 5),
    chat: Math.floor(Math.random() * 3),
    system: Math.floor(Math.random() * 2)
  };
};

// 监听路由变化，更新当前选中的消息类型
watch(
  () => route.path,
  (newPath) => {
    // 检查当前路径是否包含消息类型，如果没有则重定向到默认类型
    if (newPath.includes('/message/') && !newPath.match(/\/(like|reply|chat|system)$/)) {
      // 如果路径中只有 /message/:id 但没有具体的消息类型，重定向到 like
      router.replace(`/home/message/${route.params.id}/like`);
    }
    console.log('Current message type:', newPath);
  }
);

// 监听 id 参数变化
watch(
  () => route.params.id,
  (newId) => {
    if (newId) {
      // 当用户 ID 变化时，重新获取未读消息数量
      fetchUnreadCounts();
    }
  }
);

onMounted(() => {
  fetchUnreadCounts();
  
  // 确保初始加载时如果没有指定消息类型，则重定向到默认类型
  if (route.path === `/home/message/${route.params.id}`) {
    router.replace(`/home/message/${route.params.id}/like`);
  }
});
</script>

<style scoped>
.message-container {
  display: flex;
  height: calc(100vh - 80px);
  width: calc(100% - 20px);
  margin: 10px;
  background-color: #f8f9fa;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.message-sidebar {
  width: 260px;
  background-color: #fff;
  border-right: 1px solid #eaeaea;
  display: flex;
  flex-direction: column;
  transition: all 0.3s ease;
}

.sidebar-header {
  padding: 24px;
  border-bottom: 1px solid #f0f0f0;
  background: linear-gradient(135deg, #4a90e2, #6a5acd);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.sidebar-menu {
  padding: 16px 0;
  flex-grow: 1;
}

.menu-item {
  display: block;
  margin: 8px 12px;
  border-radius: 8px;
  color: #555;
  text-decoration: none;
  position: relative;
  transition: all 0.3s;
  overflow: hidden;
}

.menu-item-content {
  display: flex;
  align-items: center;
  padding: 14px 16px;
}

.menu-item:hover {
  background-color: #f5f7fa;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}

.menu-item.active {
  background: linear-gradient(to right, #ecf5ff, #f0f7ff);
  color: #4a90e2;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(74, 144, 226, 0.15);
}

.menu-icon {
  margin-right: 14px;
  font-size: 1.3rem;
  transition: transform 0.3s ease;
}

.menu-item:hover .menu-icon {
  transform: scale(1.1);
}

.like-icon {
  color: #ff6b6b;
}

.reply-icon {
  color: #4dabf7;
}

.chat-icon {
  color: #51cf66;
}

.system-icon {
  color: #fcc419;
}

.unread-badge {
  position: absolute;
  right: 16px;
  background: linear-gradient(135deg, #ff6b6b, #ff8787);
  color: white;
  border-radius: 12px;
  padding: 0 8px;
  font-size: 0.75rem;
  min-width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(255, 107, 107, 0.3);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

.message-content {
  flex-grow: 1;
  padding: 24px;
  overflow-y: auto;
  background-color: #fff;
}

@media (max-width: 768px) {
  .message-container {
    flex-direction: column;
  }
  
  .message-sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid #eaeaea;
  }
  
  .sidebar-menu {
    display: flex;
    padding: 10px;
  }
  
  .menu-item {
    flex: 1;
    margin: 4px;
  }
  
  .menu-item-content {
    padding: 12px 8px;
    justify-content: center;
    flex-direction: column;
  }
  
  .menu-icon {
    margin-right: 0;
    margin-bottom: 8px;
  }
  
  .unread-badge {
    position: absolute;
    top: 8px;
    right: 8px;
  }
}
</style>
